package com.linksfield.topup.config.dto;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;

/**
 * @author: songgt
 * @date: 2018-09-11 14:23
 */
@JsonIgnoreProperties(ignoreUnknown = true)

public class PageAndSort implements Serializable {

    private static final long serialVersionUID = 7780772890745670275L;

    public static final Integer MAX_PAGE_SIZE = 3000;

    public static final Integer SHORT_PAGE_SIZE = 5;


    public PageAndSort() {
        super();
        this.page = 1;
        this.rp = 10;
    }

    public PageAndSort(Integer page, Integer rp) {
        super();
        this.page = page;
        this.rp = rp;
    }

    public PageAndSort(Integer page, Integer rp, String sortName) {
        super();
        this.page = page;
        this.rp = rp;
        this.sortName = sortName;
    }

    public PageAndSort(Integer page, Integer rp, String sortName, String sortOrder) {
        super();
        this.page = page;
        this.rp = rp;
        this.sortName = sortName;
        this.sortOrder = sortOrder;
    }

    public static PageAndSort createMaxPage() {
        PageAndSort pageAndSort = new PageAndSort();
        pageAndSort.setRp(MAX_PAGE_SIZE).setPage(1);
        return pageAndSort;
    }

    public static PageAndSort generatePageAndSort(Integer page, Integer rp, String orderBy) {
        PageAndSort pageAndSort = new PageAndSort(page, rp);
        if (!StringUtils.isBlank(orderBy)) {
            String[] orderBys = orderBy.split("_");
            pageAndSort.setSortName(orderBys[0]);
            pageAndSort.setSortOrder(orderBys[1]);
        }
        return pageAndSort;
    }

    /**
     * 当前页
     */
    @ApiModelProperty(value = "当前页", position = 900, example = "1")
    private Integer page;

    /**
     * 每页显示，默认:10
     */
    @ApiModelProperty(value = "每页显示多少行", position = 910, example = "10")
    private Integer rp;

    /**
     * 总记录数
     */
    @ApiModelProperty(hidden = true)
    private Long rowCount;

    /**
     * 开始序号
     */
    @ApiModelProperty(hidden = true)
    private Long startIndex;

    /**
     * 排序字段
     */
    @ApiModelProperty(hidden = true, position = 902)
    private String sortName;

    /**
     * 排序(asc/desc)
     */
    @ApiModelProperty(hidden = true, position = 903)
    private String sortOrder = "desc";

    public static final String SORTORDER_ACS = "asc";

    /**
     * 数据开始坐标，Mysql从0开始
     */
    public Long getStartIndex() {
        return this.startIndex;
    }

    /**
     * 总页数
     */
    public Long getTotalPage() {
        if (null == rowCount) {
            return 0L;
        }
        long totalPage = (rowCount / rp);
        long remainder = rowCount % rp;
        if (rowCount > 0 && totalPage == 0) {
            totalPage = 1;
            return totalPage;
        }
        if (remainder > 0) {
            totalPage++;
            return totalPage;
        }

        return totalPage;
    }

    /**
     * 检查请求页数是否大于总页数,如果大于,设置为最后一页
     */
    private void checkPage(long totalPage) {
        if (totalPage == 0) {
            this.page = 1;
        } else if (this.getPage() > totalPage) {
            this.page = (int) totalPage;
        }
    }


    /**
     * getter and setter
     */
    public Integer getRp() {
        return rp;
    }

    public PageAndSort setRp(Integer rp) {
        this.rp = rp;
        return this;
    }

    public Integer getPage() {
        return page;
    }

    public PageAndSort setPage(Integer page) {
        this.page = page;
        return this;
    }

    public String getSortName() {
        return sortName;
    }

    public PageAndSort setSortName(String sortName) {
        this.sortName = sortName;
        return this;
    }

    public String getSortOrder() {
        return sortOrder;
    }

    public PageAndSort setSortOrder(String sortOrder) {
        this.sortOrder = sortOrder;
        return this;
    }

    public Long getRowCount() {
        return rowCount;
    }

    public PageAndSort setRowCount(Long rowCount) {
        this.rowCount = rowCount;
        checkPage(getTotalPage());
        this.startIndex = (this.getPage() - 1) * this.getRp() * 1L;
        return this;
    }

    public String getSortString() {

        if (null == sortName) {
            return null;
        }
        String[] fields = this.getSortName().split(";");
        String[] fieldsorts = this.getSortOrder().split(";");
        if (fields.length != fieldsorts.length) {
            throw new RuntimeException("排序规则不一致");
        }
        String sql = "";
        for (int index = 0; index < fields.length; index++) {
            sql = sql + " " + fields[index] + " " + fieldsorts[index];
        }
        return sql;
    }

    public PageAndSort setStartIndex(Long startIndex) {
        this.startIndex = startIndex;
        return this;
    }
}
